package club.kingyin.kyblogprogateway.core;


import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicInteger;

import java.util.Objects;

public class Distributed {

    private RedisTemplate redisTemplate;

    public Distributed(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private RedisAtomicInteger redisAtomicInteger(String key) {
        return new RedisAtomicInteger(key, Objects.requireNonNull(redisTemplate.getConnectionFactory()));
    }

    public Integer getId(String key) {
        return redisAtomicInteger(key).get();
    }

    public Integer nextId(String key) {
        return redisAtomicInteger(key).incrementAndGet();
    }

    public void setId(String key, int id) {
        redisAtomicInteger(key).set(id);
    }

}
